import { request } from '@/api/request'
import { ApiResponseData, BaseRequestMethod } from '@/api/request/types'

export function useRequest<T = ApiResponseData>(url: string): BaseRequestMethod<T> {
  // 获取数据列表
  const getDataList = (params?: any) => {
    return request.get<T>({
      url,
      params: params ?? {}
    })
  }

  // 创建数据
  const createData = (data: any) => {
    return request.post<T>({
      url,
      data
    })
  }

  // 更新数据
  const updateData = (id: number, data: any) => {
    return request.put<T>({
      url: url + '/' + id,
      data
    })
  }

  // 删除数据
  const deleteData = (id: number) => {
    return request.delete<T>({
      url: url + '/' + id
    })
  }

  return { getDataList, createData, updateData, deleteData }
}
